package com.psm.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;

import com.psm.utilities.PDFUtilities;

public class SplitPDFActionListener implements ActionListener
{
    JTextField txtSelectPDF;
    JComboBox comboSplitOptions;
    JTextField txtFromPage;
    JTextField txtToPage;
    JButton btnSplitPDF;

    public SplitPDFActionListener(JTextField txtSelectPDF, JComboBox comboSplitOptions, JTextField txtFromPage, JTextField txtToPage, JButton btnSplitPDF)
    {
	this.txtSelectPDF = txtSelectPDF;
	this.comboSplitOptions = comboSplitOptions;
	this.txtFromPage = txtFromPage;
	this.txtToPage = txtToPage;
	this.btnSplitPDF = btnSplitPDF;
    }

    @Override
    public void actionPerformed(ActionEvent ae)
    {
	if ( ae.getSource() == btnSplitPDF )
	{
	    String splitOption = (String) comboSplitOptions.getSelectedItem();
	    if ( "Split Page by page".equals( splitOption ) )
	    {
		Map< String, String > arguments = new HashMap< String, String >();
		arguments.put( "filename", txtSelectPDF.getText() );
		try
		{
		    PDFUtilities.splitPageByPage( arguments );
		}
		catch ( Exception e )
		{
		    e.printStackTrace();
		}
	    }
	    else if ( "Split Odds and Evens pages".equals( splitOption ) )
	    {

	    }
	    else if ( "Extract page".equals( splitOption ) )
	    {
		int startPage = Integer.parseInt( txtFromPage.getText() );
		int endPage = Integer.parseInt( txtToPage.getText() );
		int totalPages = endPage - startPage + 1;
		Map< String, String > config = new HashMap< String, String >();
//		config.put( "split", String.valueOf( totalPages ) );
		config.put( "startPage", String.valueOf( startPage ) );
		config.put( "endPage", String.valueOf( endPage ) );
		try
		{
		    PDFUtilities.extractPageRange( config, txtSelectPDF.getText() );
		}
		catch ( Exception e )
		{
		    e.printStackTrace();
		}
	    }
	}
    }
}
